home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / misc / libx11.lha / libX11 / lists.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-22  |  3.3 KB  |  148 lines

  1. /* Copyright (c) 1996 by Terje Pedersen.  All Rights Reserved   */
  2. /*                                                              */
  3. /* By using this code you will agree to these terms:            */
  4. /*                                                              */
  5. /* 1. You may not use this code for profit in any way or form   */
  6. /*    unless an agreement with the author has been reached.     */
  7. /*                                                              */
  8. /* 2. The author is not responsible for any damages caused by   */
  9. /*    the use of this code.                                     */
  10. /*                                                              */
  11. /* 3. All modifications are to be released to the public.       */
  12. /*                                                              */
  13. /* Thats it! Have fun!                                          */
  14. /* TP                                                           */
  15. /*                                                              */
  16.  
  17. /***
  18.    NAME
  19.      lists
  20.    PURPOSE
  21.      
  22.    NOTES
  23.      
  24.    HISTORY
  25.      Terje Pedersen - Feb 15, 1996: Created.
  26. ***/
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32. #include <exec/memory.h>
  33.  
  34. #include <memwatch.h> /* To enable memlib, you must #define MWDEBUG to 1 */
  35.  
  36. #include "lists.h"
  37.  
  38. ListNode_t *List_NewNode(void){
  39.   ListNode_t *pNew=calloc(sizeof(ListNode_t),1);
  40.   if(!pNew) X11resource_exit(sizeof(ListNode_t));
  41.   return(pNew);
  42. }
  43.  
  44. ListNode_t *List_MakeNull(void){
  45.   return(List_NewNode());
  46. }
  47.  
  48. void List_AddEntry(ListNode_t *pNode,void *pData){
  49.   ListNode_t *pNew=List_NewNode();
  50.   if( pData==0 || pNode==0 ){
  51.     printf("Warning: null entry!\n");
  52.     getchar();
  53.     if(!pNode) exit(-1);
  54.   }
  55.   pNew->pNext=pNode->pNext;
  56.   pNode->pNext=pNew;
  57.   pNew->pData=pData;
  58. }
  59.  
  60. void List_GetNext(ListNode_t **pNode){
  61.   *pNode=(*pNode)->pNext;
  62. }
  63.  
  64. void List_AddLast(ListNode_t *pNode,void *pData){
  65.   ListNode_t *pNew,*pPrev=pNode;
  66.  
  67.   if( pData==0 || pNode==0 ){
  68.     printf("Warning: null entry!\n");
  69.     getchar();
  70.     if(!pNode) exit(-1);
  71.   }
  72.  
  73.   while( pNode->pNext ){
  74.     pPrev=pNode;
  75.     pNode=pNode->pNext;
  76.   }
  77.  
  78.   pNew=List_NewNode();
  79.   pNew->pNext=pPrev->pNext;
  80.   pPrev->pNext=pNew;
  81.   pNew->pData=pData;
  82. }
  83.  
  84. void List_FreeList(ListNode_t *pNode){
  85.   ListNode_t *pNext;
  86.   pNext=pNode->pNext;
  87.   if(!pNode){
  88.     printf("Freeing null list!\n");
  89.     exit(-1);
  90.   }
  91.   free(pNode);
  92.   pNode=pNext;
  93.   while(pNode){
  94.     pNext=pNode->pNext;
  95.     if(pNode->pData) free(pNode->pData);
  96.     free(pNode);
  97.     pNode=pNext;
  98.   }
  99. }
  100.  
  101. void List_RemoveEntry(ListNode_t *pNode,void *pData){
  102.   ListNode_t *pPrev;
  103.   if(!pNode){
  104.     printf("Freeing entry from empty list!\n");
  105.     exit(-1);
  106.   }
  107.   while(pNode && pNode->pData!=pData){
  108.     pPrev=pNode;
  109.     pNode=pNode->pNext;
  110.   }
  111.  
  112.   if(!pNode){
  113.     return;
  114.   }
  115.   if(pNode->pData==pData){
  116.     pPrev->pNext=pNode->pNext;
  117.     if(pNode->pData)
  118.       free(pNode->pData);
  119.     free(pNode);
  120.   }
  121.   else {
  122.     printf("removing nonexistent entry\n");
  123.     getchar();
  124.   }
  125.  
  126. }
  127.  
  128. void List_RemoveNode(ListNode_t *pNode,void *pData){
  129.   ListNode_t *pPrev;
  130.   if(!pNode){
  131.     printf("Freeing entry from empty list!\n");
  132.     exit(-1);
  133.   }
  134.   while(pNode && pNode->pData!=pData){
  135.     pPrev=pNode;
  136.     pNode=pNode->pNext;
  137.   }
  138.  
  139.   if(!pNode){
  140.     return;
  141.   }
  142.  
  143.   if(pNode->pData==pData){
  144.     pPrev->pNext=pNode->pNext;
  145.     free(pNode);
  146.   }
  147. }
  148.